//global variables
var top_init = 0;
var is_shop_selected = false;
var history_count = 0;

function post_shopcomment(shopid,is_key){
    var keycode;
    if (is_key){
        if (window.event) keycode = window.event.keyCode;//IE
        else if (e) keycode = e.which;//other
        else return true;
    
        if (keycode != 13){
            return true;    
        }
    }
    var content = $("#id_shopcomment_content").val();
    $.post("/outland/list/post_comment",{shop:shopid,content:content},
    function(data){
        $.getJSON(url,junk,
        function(data){
            var newComment = document.getElementById("shopcomment");
            var cmt = "<table id='tcomment'><tr><td>Shop Comments</td></tr>";
            $.each(data.comment, function(){
                cmt+= "<tr><td>"+this.user+" "+this.time+"<br/>"+this.content+"<hr></td></tr>";
 
            });
            cmt += "<tr><td>\
            <input id='id_comment_shop' type='hidden' name='shop' value="+data.shop+" />\
            <input id='id_shopcomment_content' type='text' name='content' maxlength='500' style='width:680px' onKeyPress='return post_shopcomment(" + data.shop +",1);' >\
            <input type='button' value='쓰기' onclick='post_shopcomment(" + data.shop +",0);'></td></tr></table></table>";
            newComment.innerHTML = cmt;
        });
    },"html");
    
    return false;
}
function handle_shoplist () {
    //click trigger for each shop
    $('#shop_list li').click(function() {
        $(this).find('a').trigger('click');
    });
    //when a shop is clicked
    $('#shop_list li a').click(function() {
        //display menulist
        is_shop_selected = true;
        this_id = $(this).attr("id");
        uri = $(this).attr("href");
        uri2 = $(this).attr("name");
        if (uri == '#' || uri == '') {
            $('#shop_detail').html('업체를 선택하세요');
        }
        else {
            
	    $.history.add('s'+uri2);
	    /*
            $.ajax({
                url: uri2,
                cache: false,
                success: function(data) {
                    $('#menulist tbody').html(data);
		    addCartHandler();
                }
            });
	    */
        }
        //display shop comments
        
        url = "/outland/list/json/shopcomment/"+this_id;
        junk='';
        $.getJSON(url,junk,
        function(data){
            var newComment = document.getElementById("shopcomment");
            var cmt = "<table id='tcomment'><tr><td>Shop Comments</td></tr>";
            $.each(data.comment, function(){
                cmt+= "<tr><td>"+this.user+" "+this.time+"<br/>"+this.content+"<hr></td></tr>"; 
            });
            cmt += "<tr><td>\
            <input id='id_comment_shop' type='hidden' name='shop' value="+data.shop+" />\
            <input id='id_shopcomment_content' type='text' name='content' maxlength='500' style='width:680px' onKeyPress='return post_shopcomment(" + data.shop +",1);' >\
            <input type='button' value='쓰기' onclick='post_shopcomment(" + data.shop +",0);'></td></tr></table></table>";
            newComment.innerHTML = cmt;
        });
        return false;
    });
    
    //display shop detail description when mouse over event
    $('#shop_list ul li').mouseover(function(){
        if(!is_shop_selected){
            url=$('a',this).attr('href');
            $.ajax({
                url: url,
                cache: false,
                success: function(data) {
                    $('#shop_detail').html(data);
                }
            });
        }
    });

}

$(document).ready(function(){

    $('#menulist td').click(function(){
      $(this).find('a').trigger('click');
    });
    $('#menulist tbody tr').hover(
      function () {
        $(this).addClass('hover');
      }, 
      function () {
        $(this).removeClass('hover');
      }
    );

    pos_init = $('#cart').position();
    top_init = pos_init.top;
    $(window).scroll(function(){
        var pos = $('#cart').position();
        var scrtop = $(window).scrollTop();
        //alert(pos.top + ',' + top_init);
        if (pos.top < top_init)
        {
            $('#cart').css({
                position: 'static',
                top: top_init-scrtop
            });
        }
        if (pos.top - scrtop < 30)
        {
            $('#cart').css({
                position: 'fixed',
                top: 30+'px'
            });

        }
    });
    
    handle_shoplist();

    //click trigger for each category
    $('#categories li').click(function() {
        $(this).find('a').trigger('click');
    });
    
    //display shop list when a category is clicked
    $('#categories li a').click(function() {
        is_shop_selected = false;
        uri = $(this).attr("href");
        if (uri == '#' || uri == '') {
            $('#shop_list').html('카테고리를 선택하세요.');
        }
        else {
	    $.history.add('c'+uri);
	    /*
            $.ajax({
                url: uri,
                cache: false,
                success: function(data) {
                    $('#shop_list').html(data);
                    handle_shoplist();
                }
            });
            $(this).parent().siblings().removeClass('active')
            $(this).parent().addClass('active');
	    */
        }
        return false;
    });
});
